package com.example.entity;

import lombok.Data;

import java.time.LocalDateTime;

@Data
public class ResultMsg<T> {

    private T data;
    private String msg;
    private LocalDateTime time;
    private int code;
    private boolean success;

    public static <T> ResultMsg<T> success() {
        return success(null);
    }

    public static <T> ResultMsg<T> success(T data) {
        return success(data, "ok");
    }

    public static <T> ResultMsg<T> success(T data, String message) {
        return getInstance(data, message, 200, true);
    }

    public static <T> ResultMsg<T> success(T data, String message, int code) {
        return getInstance(data, message, code, true);
    }

    public static <T> ResultMsg<T> error() {
        return error(null);
    }

    public static <T> ResultMsg<T> error(T data) {
        return error(data, "error");
    }

    public static <T> ResultMsg<T> error(T data, String message) {
        return getInstance(data, message, 500, false);
    }

    public static <T> ResultMsg<T> error(T data, String message, int code) {
        return getInstance(data, message, code, false);
    }

    private static <T> ResultMsg<T> getInstance(T data, String message, int code, boolean success) {
        ResultMsg<T> resultMsg = new ResultMsg<T>();
        resultMsg.setData(data);
        resultMsg.setMsg(message);
        resultMsg.setCode(code);
        resultMsg.setSuccess(success);
        resultMsg.setTime(LocalDateTime.now());
        return resultMsg;
    }
}
